<?php

class ConfigAreas extends Controller{

    private $_data;
    
    public function __construct(){
        parent::__construct();
        $this->_data = Array();
    }

    private function _render () {
        $this->_data['title'] = 'Gestión de &aacute;reas';
        $this->_data['page'] = 'configAreas';
        
        $this->_view->rendertemplate('header',$this->_data);
        $this->_view->rendertemplate('menu',$this->_data);
        $this->_view->render('config/areas',$this->_data);
        $this->_view->rendertemplate('footer',$this->_data);
    }
    
    public function index(){
        $this->_data['page_section'] = 'main';
        //$this->_data['attributes'] = $this->_model->getAttributes ();
        $this->_render();
    }

    public function areas () {
        $idScheme = $_GET['idScheme'];
        if ($idScheme == "") {
            $this->_view->setOutput (json_encode (Array()));
            return;
        }
        
        $attributes = $this->_model->getAreas ($idScheme);  
        $this->_view->setOutput (json_encode ($attributes));
    }
        
    public function addArea () {
        $response = Array();
        
        if (!isset ($_POST['name']) || !isset ($_POST['order']) || !isset ($_POST['idScheme'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $name = $_POST['name'];
            $order = $_POST['order'];
            $idScheme = $_POST['idScheme'];
            
            if ($this->_model->addArea ($name, $order, $idScheme) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function modifyArea () {
        $response = Array();
        
        if (!isset ($_POST['id']) || !isset ($_POST['name']) || !isset ($_POST['order'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $id = $_POST['id'];
            $name = $_POST['name'];
            $order = $_POST['order'];
            
            if ($this->_model->modifyArea ($id, $name, $order) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function removeArea () {
        $response = Array();
        
        if (!isset ($_POST['id'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $id = $_POST['id'];
            
            if ($this->_model->removeArea ($id) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }

}

?>